package com.zt.bigdata.helloclient;

import com.zt.bigdata.document.Customer;
import com.zt.bigdata.helloclient.mongo.MongoUserService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * @Author: zhangtong
 * @Date: 2018/9/14 上午9:18
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class Main {

    private static final Logger logger = LoggerFactory.getLogger(Main.class);
    @Autowired
    MongoUserService mongoUserService;


    @Before
    public void before() {
        for (int i = 0; i < 10; i++) {
            Customer customer = new Customer();

            customer.setName("name" + i % 3);
            customer.setAddress("hangzhou 第" + i % 4 + "区");
            customer.setAge(20 + i);
            customer.setSex(i % 2 == 0 ? true : false);
            customer.setDelFlag(false);
            mongoUserService.insert(customer);
            logger.info("插入用户 " + i);
        }
    }

    @Test
    public void contextLoads() {
        List<Customer> customers = mongoUserService.findAllByAddress("hangzhou 第2区");
        logger.info("查找第二区客户 customers:{}", customers);

        PageRequest pageable = PageRequest.of(0, 5);
        Page<Customer> page = mongoUserService.findByAge(23, pageable);
        logger.info("分页查找23岁 page:{},size:{}, content:{}", page.getNumber(), page.getSize(), page.getContent());

        pageable = PageRequest.of(1, 5);
        page = mongoUserService.findByAgeGreaterThan(20, pageable);
        logger.info("分页查找大于20岁 page:{},size:{}, content:{}", page.getNumber(), page.getSize(), page.getContent());

        page = mongoUserService.findByNameLike("name", pageable);
        logger.info("模糊查询名字 page:{},size:{}, content:{}", page.getNumber(), page.getSize(), page.getContent());

        Customer customer = mongoUserService.findOneByAge(20);

        logger.info("And 查询 customer:{}", customer);
    }

    @After
    public void after() {
        mongoUserService.deleteAllByDelFlag(false);
    }
}